﻿using System;
using GdNet.Core.Assistants;
using GdNet.Integrations.NHibernate.Assistants;
using GdNet.Integrations.NHibernate.Mapping;
using GdNet.WebFramework.Domain.ContentManagement;
using GdNet.WebFramework.Domain.ReferencesData;
using NHibernate.Mapping.ByCode;

namespace GdNet.WebFramework.Mapping.ContentManagement
{
    public class ContentItemMapping : AbstractJoinedSubclassMapping<ContentItem, Guid>, IEntityMapping
    {
        public ContentItemMapping()
            : base()
        {
            base.Property(e => e.Name);
            base.Property(e => e.Description);
            base.Property(e => e.Keywords);

            base.ManyToOne(e => e.Culture, m =>
            {
                m.Lazy(LazyRelation.Proxy);
                m.Access(Accessor.Property);
                m.Cascade(Cascade.None);
                m.Column(MappingAssistant.GetForeignKeyColumn<ContentItem, DataLine>(ci => ci.Culture));
            });

            base.List(e => e.Parts, cm =>
            {
                cm.Access(Accessor.Field);
                cm.Cascade(Cascade.All | Cascade.DeleteOrphans);
                cm.Lazy(CollectionLazy.Lazy);
                cm.Key(km => km.Column(MappingAssistant.GetForeignKeyColumn<ContentPart, ContentItem>(cp => cp.ContentItem)));
                cm.Index(idx =>
                {
                    idx.Base(1);
                    idx.Column(ObjectAssistant.GetPropertyName<ContentPart, int>(cp => cp.Position));
                });
            }, m => m.OneToMany(mapping => mapping.NotFound(NotFoundMode.Exception)));
        }
    }
}
